package com.example.time.handler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.Date;

/**
 * @author linfeng
 * @date 2021/12/17 10:52
 */
public class TimeClientHandler extends ChannelInboundHandlerAdapter {


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf byteBuf = (ByteBuf) msg;
        try {
            // 这里有可能抛出IndexOutOfBoundsException
            // 因为当前接收到的数据可能不足8个字节
            long timestamp = byteBuf.readLong();
            Date date = new Date(timestamp);
            System.out.println("时间：" + date);

            ctx.close();
        } finally {
            byteBuf.release();
        }
    }
}
